package cn.tedu.boot22.controller;

import cn.tedu.boot22.utils.DBUtils;
import cn.tedu.boot22.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.*;
import java.util.ArrayList;

@Controller
public class UserController {

    @RequestMapping("/reg")
    @ResponseBody
    public String rge(User user) {
        System.out.println("user = " + user);
        //得到数据库连接
        try (
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "select id from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return "用户已存在";
            }
            //用户名不存在，可以注册
            String insertSql = "insert into user values(null, ?, ?, ?)";
            PreparedStatement insertPs = conn.prepareStatement(insertSql);
            insertPs.setString(1,user.getUsername());
            insertPs.setString(2,user.getPassword());
            insertPs.setString(3,user.getNick());
            insertPs.executeUpdate();//执行
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return "注册成功";
    }

    @RequestMapping("/login")
    @ResponseBody
    private String login(User user){
        System.out.println("user = " + user);
        //获取连接
        try (
                Connection conn = DBUtils.getConn();
                ){
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                //得到查询回来的密码
                String pw = rs.getString(1);
                if (pw.equals(user.getPassword())){
                    return "登录成功";
                }
                return "密码错误!登录失败";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "登录失败";
    }


    @RequestMapping("/showAllUser")
    @ResponseBody
    public String showAllUser(){
        ArrayList<User> list = new ArrayList<>();
        try (
                Connection conn = DBUtils.getConn();
                ){
            String sql = "select username,password,nick from user";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String username = rs.getString(1);
                String password = rs.getString(2);
                String nick = rs.getString(3);
                User user = new User(username,password,nick);
                list.add(user); //把查询到的对象装进集合
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        //遍历集合把里面的数据装进一个html里面
        String html = "<table border=1>";
        html+="<caption>用户列表</caption>";
        html+="<tr><th>用户名</th><th>密码</th><th>昵称</th></tr>";
        for (User user: list) {
            html+="<tr>";
            html+="<td>"+user.getUsername()+"</td>";
            html+="<td>"+user.getPassword()+"</td>";
            html+="<td>"+user.getNick()+"</td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

}
